package com.ruoyi.yuce.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.yuce.domain.pojo.CoBuJian;
import com.ruoyi.yuce.domain.pojo.CoBuJianCanShu;
import com.ruoyi.yuce.domain.pojo.CoChangZhan;
import com.ruoyi.yuce.service.IBujiancanshuService;
import com.ruoyi.yuce.service.IChangzhanService;
import com.ruoyi.yuce.service.IFengjiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuce.domain.FaultRecord;
import com.ruoyi.yuce.service.IFaultRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 2020年1月故障记录Controller
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@RestController
@RequestMapping("/ruoyi-yuce/faultrecord")
@DataSource(value = DataSourceType.SLAVELS)
public class FaultRecordController extends BaseController
{
    @Autowired
    private IFaultRecordService faultRecordService;

    @Autowired
    private IChangzhanService changzhanService;

    @Autowired
    private IFengjiService fengjiService;

    @Autowired
    private IBujiancanshuService bujiancanshuService;

    /**
     * 查询故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultRecord faultRecord)
    {
        startPage();
        Date date = Date.valueOf(faultRecord.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        faultRecord.setCriqi(time);
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        return getDataTable(list);
    }

    /**
     * 查询场站名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:getChangZhanName')")
    @GetMapping("/getChangZhanName")
    public List<CoChangZhan> getChangZhanName()
    {
        logger.info("getChangZhanName function ...");
        List<CoChangZhan> changZhanName = changzhanService.getChangZhanName();
        return changZhanName;
    }

    /**
     * 查询部件名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:getBuJian')")
    @GetMapping("/getBuJian")
    public List<CoBuJian> getBuJian() {
        logger.info("getBuJian function ...");
        List<CoBuJian> buJian = fengjiService.getBuJian();
        return buJian;
    }
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:getBuJian1')")
    @GetMapping("/getBuJian1")
    public List<CoBuJian> getBuJian1(@RequestParam(value="czid",required=false) int czid) {
        logger.info("getBuJian function ...");
        int n = czid;
        List<CoBuJian> buJian = fengjiService.getBuJian1(czid);
        return buJian;
    }

    /**
     * 查询部件参数名
     * @return
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:getBuJianLeiXing')")
    @GetMapping("/getBuJianCanShu")
    public List<CoBuJianCanShu> getBuJianLeiXing()
    {

        logger.info("getBuJianLeiXing function ...");
        List<CoBuJianCanShu> buJianCanShu = bujiancanshuService.getBuJianCanShuName();
        return buJianCanShu;
    }


    /**
     * 导出2020年1月故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:export')")
    @Log(title = "风机故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultRecord faultRecord)
    {
        Date date = Date.valueOf(faultRecord.getCriqi());
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String time="csczevent"+sf.format(date);
        faultRecord.setCriqi(time);
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        ExcelUtil<FaultRecord> util = new ExcelUtil<FaultRecord>(FaultRecord.class);
        util.exportExcel(response, list, "风机故障记录数据");
    }

    /**
     * 获取风机故障记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(faultRecordService.selectFaultRecordById(id));
    }

    /**
     * 新增2020年1月故障记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:add')")
    @Log(title = "新增故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.insertFaultRecord(faultRecord));
    }

    /**
     * 修改2020年1月故障记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:edit')")
    @Log(title = "修改故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.updateFaultRecord(faultRecord));
    }

    /**
     * 删除2020年1月故障记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:faultrecord:remove')")
    @Log(title = "删除故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(faultRecordService.deleteFaultRecordByIds(ids));
    }
}
